// pages/home/home.js
//获取挂载的全局数据
// let obj = getApp();
Page({
    data: {
        dataList: [],
        balance: 0, //本月结余
        comein: '0.00', //本月收入
        spend: '0.00' //本月支出
    },
    init() {
        let app = getApp();
        this.setData({
            dataList: app.globalData.dataList
        })
        console.log(app.globalData.dataList);

        let count = app.globalData.dataList;
        //初始值为0
        let earn = 0;
        let spend = 0;
        // console.log(spend,earn);
        //本月收入与支出
        let that = this;
        for (let i = 0; i < count.length; i++) {
            let dayily = count[i].daily;
            // console.log(dayily,111);
            //判断类型进行自增
            if (dayily == "收入") {
                let num = Number(count[i].money);
                earn += num;
            }
            if (dayily == "支出") {
                let money = Number(count[i].money);
                spend += money;
            }
            that.setData({
                comein: earn,
                spend: spend
            })
            // console.log(spend,"spend")
            // console.log(earn,"earn")
            //本月结余,取整
            let total = earn - spend;
            total = parseInt(total);
            // console.log(total,"total")
            that.setData({
                balance: total
            })
        }
        // console.log("count",count);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        // 初始化加载
        wx.showLoading({
            title: '加载中',
        })
        setTimeout(function () {
            wx.hideLoading()
        }, 500)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.init();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})